﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace lab2
{
    public partial class addBilet : Form
    {
        OleDbConnection cn;
        OleDbCommand cm;
        OleDbDataAdapter da;
        DataTable dt;

        public addBilet()
        {
            InitializeComponent();
        }

        public void setPass()
        {

            cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\\studying\\6sem\\БД\\Student\\passengers.mdb");
            cn.Open();
            cm = new OleDbCommand("SELECT * FROM пассажиры", cn);
            da = new OleDbDataAdapter();
            da.SelectCommand = cm;
            dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;

            cn.Close();
            cm.Dispose();
            da.Dispose();
            dt.Dispose();
        }
        public void setReis()
        {

            cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\\studying\\6sem\\БД\\Student\\passengers.mdb");
            cn.Open();
            cm = new OleDbCommand("SELECT * FROM рейсы", cn);
            da = new OleDbDataAdapter();
            da.SelectCommand = cm;
            dt = new DataTable();
            da.Fill(dt);
            dataGridView2.DataSource = dt;

            cn.Close();
            cm.Dispose();
            da.Dispose();
            dt.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int idPass;
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow currentRow = dataGridView1.SelectedRows[0];
                idPass = Convert.ToInt32(currentRow.Cells[0].Value);
            }
            else
            {
                idPass = 0;
            }

            int idreis;
            if (dataGridView2.SelectedRows.Count > 0)
            {
                DataGridViewRow currentRow = dataGridView2.SelectedRows[0];
                idreis = Convert.ToInt32(currentRow.Cells[0].Value);
            }
            else
            {
                idreis = 0;
            }

            if (textBox1.Text.ToString() == "" || idreis == 0 || idPass == 0)
            {
                errorForm frm = new errorForm();
                frm.Show();
            }
            else
            {
                cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\\studying\\6sem\\БД\\Student\\passengers.mdb");
                cn.Open();


                string test = "INSERT INTO Билеты ( `seat`, id_passenger, `id_flight` ) VALUES ('" + textBox1.Text.ToString() + "', '" + idPass + "', '" + idreis + "')";
                cm = new OleDbCommand(test, cn);
                cm.ExecuteNonQuery();
                cm.Dispose();
                cn.Close();
                cm.Dispose();
                this.Close();
            }
        }
    }
}
